package com.example.leoss.zhuye.search;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.database.Cursor;
import android.widget.TextView;
import android.widget.Toast;
import com.example.leoss.Duquzifu.Data;
import com.example.leoss.Duquzifu.FileUtils;
import com.example.leoss.R;
import com.example.leoss.zhuye.ParseXls.Caozuo_teacher;
import java.util.ArrayList;

public class SearchActivity extends AppCompatActivity {
    Caozuo_teacher caozuo;
    Cursor mCursor;
    private SearchView searchView;
    ArrayList<Data> list,mListSearch;
    private SearchAdpat mAdapter;
    private ListView id_tongxun_lv1s;
    private String contact_name;
    private String contact_phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);


getSupportActionBar().hide();
        caozuo = new Caozuo_teacher(SearchActivity.this);
        // 3. 绑定组件
        searchView = (SearchView) findViewById(R.id.search_view);
        id_tongxun_lv1s=findViewById(R.id.list_item);
        //数据库读取
        mCursor = caozuo.xianshi_all();

        list = new ArrayList<>();
        while (mCursor.moveToNext())
        {
            int id=Integer.valueOf(mCursor.getString(0));//id
            String  name= mCursor .getString(1);
            String sex=mCursor.getString(2);
            String address=mCursor.getString(3);
            String major=mCursor.getString(4);
            String  phome= mCursor .getString(5);
            String qq=mCursor.getString(6);
            String weixing=mCursor.getString(7);
            list.add(new Data(id,name ,phome,SearchActivity.this,qq,weixing,address,major,sex, FileUtils.getFileIconByPath(sex))); // 亳[bó]属于不常见的二级汉字
        }


        mAdapter= new   SearchAdpat(list,SearchActivity.this);
        id_tongxun_lv1s.setAdapter(mAdapter);

        id_tongxun_lv1s.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
            {
                Toast.makeText(SearchActivity.this, String.valueOf(i), Toast.LENGTH_SHORT).show();

                TextView name = view.findViewById(R.id.id_layouttx_tv_name1);
                TextView phone = view.findViewById(R.id.id_layouttx_tv_phone1);
                //Imgvie点击事件
                ImageView img=view.findViewById(R.id.img_view_layout_phome1);

                contact_name = name.getText().toString();//读取姓名
                contact_phone = phone.getText().toString();//读取电话号码

                confirm_call();

            }
        });


        id_tongxun_lv1s.setTextFilterEnabled(true);

        // 4. 设置点击搜索按键后的操作（通过回调接口）
        // 参数 = 搜索框输入的内容
        searchView.setOnClickSearch(new ICallBack() {
            @Override
            public void SearchAciton(String s) {


                if (!TextUtils.isEmpty(s)) {
            String text = s.toString().trim();
            mListSearch=new ArrayList<>();
            if (!TextUtils.isEmpty(text)) {

                if (mListSearch != null && mListSearch.size() > 0) {
                    // mListSearch 模糊搜索结果集合
                    mListSearch.clear();
                }

                // mList 需要模糊搜索的集合
                for (Data bean : list) {
                    // 判断javabean中是否包含搜索字段
                    if (bean.getName().contains(text)||bean.getPhome().contains(text)) {
                        // 若包含，添加
                        mListSearch.add(bean);
                    }
                }
                // 给RecycleView设置搜索结果
                mAdapter = new SearchAdpat(mListSearch,SearchActivity.this);
                id_tongxun_lv1s.setAdapter(mAdapter);
            }
        } else {
            // 输入框输入字段为空
            mListSearch.clear();
            mAdapter = new SearchAdpat(list,SearchActivity.this);
            id_tongxun_lv1s.setAdapter(mAdapter);
        }

            }
        });

        // 5. 设置点击返回按键后的操作（通过回调接口）
        searchView.setOnClickBack(new bCallBack() {
            @Override
            public void BackAciton() {
                finish();
            }
        });

    }
    private AlertDialog alert = null;
    private void confirm_call()
    {
        alert = null;
        AlertDialog.Builder builder = new AlertDialog.Builder(SearchActivity.this);
        alert = builder.setIcon(R.drawable.img_logo)
                .setTitle("系统提示：")
                .setMessage("确定拨打"+contact_name+"的电话?")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(SearchActivity.this, "你点击了取消按钮~", Toast.LENGTH_SHORT).show();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        call_phone(contact_phone);
                        Toast.makeText(SearchActivity.this, "你点击了确定按钮~", Toast.LENGTH_SHORT).show();
                    }
                }).create();


        alert.show();

    }

    private void call_phone(String phone_number)
    {
        //获取输入的电话号码
        //创建打电话的意图
        if(ContextCompat.checkSelfPermission(SearchActivity.this, Manifest.permission.CALL_PHONE)!= PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(SearchActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);
        }else
        {
            Intent intent = new Intent();
            //设置拨打电话的动作
            intent.setAction(Intent.ACTION_CALL);
            //设置拨打电话的号码
            intent.setData(Uri.parse("tel:" + phone_number));
            //开启打电话的意图
            startActivity(intent);
        }
    }
}
